package com.uchengwang.ui.cm_ui.uu.ui;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UserConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.UserBeanList;
import com.uchengwang.ui.cm_ui.uu.adapter.AddAdapter;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.PopupUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2018/5/24.
 * 搜索好友
 */

public class SearchFriendActivity extends BaseWithTitleActivity{
    private LinearLayout search_search;
    private RecyclerView search_rv;
    private AddAdapter addAdapter;
    private EditText search_search_text;
    private View view;
    private TextView popup_search;//搜索显示
    private PopupUtils popupUtils;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_search_friend);

        setTitle("搜索");


        setLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UCommonUtil.getUtil().hideSoftInput(SearchFriendActivity.this, search_search_text);
                SearchFriendActivity.this.finish();
            }
        });

        initView();

    }

    private void initView(){
        search_search = (LinearLayout) findViewById(R.id.search_search);
        search_rv = (RecyclerView) findViewById(R.id.search_rv);

        search_rv.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

        search_search_text = (EditText) findViewById(R.id.search_search_text);
        search_search_text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (popupUtils != null){
                    popupUtils.dismiss();
                }
                String k = search_search_text.getText().toString();
                if (TextUtils.isEmpty(k)){
                    return true;
                }
                getFriends(search_search_text.getText().toString());

                return true;
            }
        });
        view = LayoutInflater.from(this).inflate(R.layout.popup_view, null, false);

        search_search_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                if (s.toString() != null && s.length() > 0) {

                    if (view == null){
                        view = LayoutInflater.from(SearchFriendActivity.this).inflate(R.layout.popup_view, null, false);
                    }
                    popup_search = (TextView) view.findViewById(R.id.popup_search);
                    popup_search.setText(s.toString());
                    popup_search.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            popupUtils.dismiss();


                            getFriends(search_search_text.getText().toString());
                        }
                    });
                    if (popupUtils == null || !popupUtils.isShow()) {
                        popupUtils = PopupUtils.getUtils().showView(SearchFriendActivity.this, view, false, false, false);
                        popupUtils.showNo(search_search);
                    }
                } else {
                    popupUtils.dismiss();

                }

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


        initData();
    }

    private void initData(){
        search_rv.setLayoutManager(new LinearLayoutManager(this));
        addAdapter = new AddAdapter(this);
        search_rv.setAdapter(addAdapter);

    }




    /**
     * 获取好友
     * @param key 搜索关键词
     * */
    private void getFriends(String key){
        MapRequest map =new MapRequest();
        if (!TextUtils.isEmpty(key)){
            map.put(BaseConfig.KEY_WORD, key);
        }

        map.put(BaseConfig.PAGE, 1);
        map.put(BaseConfig.PERPAGE, 85);
        LoadingUtils.getUtils().loading(this);


        HttpManager.newInstance(this).query(UserConfig.ChangeInfo.method_check, UserConfig.ChangeInfo.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().hideSoftInput(SearchFriendActivity.this, search_search_text);
                Log.d("jjf", "status="+status+"=msg="+msg);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();

                UCommonUtil.getUtil().hideSoftInput(SearchFriendActivity.this, search_search_text);

                if (status != 0){
                    addAdapter.getData().clear();
                    addAdapter.notifyDataSetChanged();
                    UCommonUtil.getUtil().showToast(SearchFriendActivity.this, msg);
                    return;
                }
                if (!TextUtils.isEmpty(data) && !data.equals("[]")){
                    UserBeanList listBean = JsonUtils.getManager().stringToBean(data, UserBeanList.class);
                    List<UserBean> list = listBean.getData();

                    addAdapter.setData(list);

                }
            }
        });
    }


}
